//
//  SearchViewController.m
//  AngelaBaby
//
//  Created by lanou3g on 15/10/14.
//  Copyright © 2015年 Jenney. All rights reserved.
//

#import "SearchViewController.h"
#import "dataHelper.h"
#import "ListModel.h"
#import "SearchTableViewCell.h"
#import "DetailModel.h"


#define KURL @"http://app.9nali.com/805/albums/306290"
//#define KDIC {"page_id":"%@"}
#define KRECOMMENDURL @"http://app.9nali.com/805/common_tag/6/all"

@interface SearchViewController ()<UISearchResultsUpdating,UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UISearchController *searchController;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *filterArray;

@property(nonatomic,strong)NSMutableArray *baseArray;

@property(nonatomic,strong)NSMutableArray *recommendArray;

@property(nonatomic,strong)dataHelper *dataHandel;

@property(nonatomic,strong) DetailModel *detailModel;

@property(nonatomic,strong)ListModel *listModel;

@property(nonatomic,strong)SearchTableViewCell *searchView;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout =UIRectEdgeNone;
    [self setSearchViewController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setSearchViewController{
    //设置代理
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(returnTo:)];
    
    self.tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
    self.searchController.active=YES;
    self.searchController.searchResultsUpdater=self;
    self.searchController.searchBar.delegate=self;
self.searchController.searchBar.placeholder=@"搜索";
    self.searchController.hidesNavigationBarDuringPresentation=NO;
    [self.searchController.searchBar sizeToFit];
    [self.navigationItem setTitleView:self.searchController.searchBar];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SearchTableViewCell" bundle:nil] forCellReuseIdentifier:@"identifier"];
//    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseCell"];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
   
  
    [self.view addSubview:_tableView];
    
    _dataHandel =[dataHelper shareDataHelper];
    
    
    _detailModel=[[DetailModel alloc]init];
    self.baseArray =[NSMutableArray array];
        [_dataHandel getDataWithUrl:KURL dic:@{@"page_id":@1} model:_detailModel returnArray:^(NSMutableArray *arra) {
            _baseArray =arra;
           
        }];
    self.filterArray =[NSMutableArray array];
    
    self.recommendArray =[NSMutableArray array];
    _listModel =[[ListModel alloc]init];
    [_dataHandel getDataWithUrl:KRECOMMENDURL dic:@{@"page_id":@1} model:_listModel returnArray:^(NSMutableArray *arra) {
        _recommendArray=arra;
    }];
}


-(void)returnTo:(UIBarButtonItem *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];

}



#pragma mark ---searchBar delegate

//-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
//   
//
//    return YES;
//}


#pragma mark ---searchController delegate

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    if (self.filterArray!=nil) {
        [self.filterArray removeAllObjects];
    }
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF.title CONTAINS[c] %@",self.searchController.searchBar.text];
    self.filterArray=[self.baseArray filteredArrayUsingPredicate:predicate].mutableCopy;
    [self.tableView reloadData];
}


#pragma mark ---tableViewDelegate
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//
//}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.searchController.active) {
        UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"reuseCell" forIndexPath:indexPath];
        if (!cell) {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseCell"];
        }
         self.tableView.alpha =0.5;
        self.tableView.backgroundColor=[UIColor whiteColor];
        _detailModel=_filterArray[indexPath.row];
        cell.textLabel.text =_detailModel.title;
        return cell;                                    }
    else{
        SearchTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"identifier"];
         self.tableView.alpha =1;
        _listModel=_recommendArray[indexPath.section*6];
            [cell.firstBtn setTitle:_listModel.title forState:UIControlStateNormal];
        _listModel=_recommendArray[indexPath.section*6+1];
            [cell.secondBtn setTitle:_listModel.title forState:UIControlStateNormal];
        _listModel=_recommendArray[indexPath.section*6+2];
            [cell.thirdBtn setTitle:_listModel.title forState:UIControlStateNormal];
        _listModel=_recommendArray[indexPath.section*6+3];
            [cell.fourthBtn setTitle:_listModel.title forState:UIControlStateNormal];
        _listModel=_recommendArray[indexPath.section*6+4];
            [cell.fiveBtn setTitle:_listModel.title forState:UIControlStateNormal];
        _listModel=_recommendArray[indexPath.section*6+5];
            [cell.sixBtn setTitle:_listModel.title forState:UIControlStateNormal];
        
        return cell;
    }
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.searchController.active)
        return 100;
  else
      return 20;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (!self.searchController.active) {
        return 10;
    }
    else
        return 0;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (!self.searchController.active) {
        if (section==0) {
            return @"热门搜索";
        }
        else
            return nil;
    }else
        return nil;
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchController.active) {
        return _filterArray.count;
    }else{
        return 1;
    }
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (!self.searchController.active) {
        return _recommendArray.count/6;
    }else
        return 1;
}




@end
